/**
 * Created by Administrator on 15-7-1.
 */
var dialog = new Dialog({container:$("#image-container")});
var WelcomeReply = RichBase.extend({
	statics : {
		defaultImg : "http://www.12301.cc/wx_public/images/nopic.jpg",
		type : "EVENT_SUBSCRIBE"
	},
	EVENTS : {
		"click" : {
			"#welSwitchTabNavWrap .welTabNav" : "onWelRadioClick",
			"#submitBtn_welcome" : "onSubmit",
			"#addWelReplyBoxBtn" : "onAddWelReplyBox",
			"#welReplyWrap .removeBtn" : "onRemoveWelReplyBox",
			"#welReplyWrap .openDialogBtn" : "onOpenDialog"
		},
		"focus" : {
			"#welReplyWrap .sformInp" : "onVerifyInpFocus"
		}
	},
	getHash : function(){ return $("#hashHidInp").val()},
	init : function(){
		this.submitBtn = $("#submitBtn_welcome");
	},
	onWelRadioClick : function(that,e){
		var tarTab = $(e.currentTarget);
		if(tarTab.hasClass("active")) return false;
		tarTab.addClass("active").siblings().removeClass("active");
		that.container.children(".panel-body").children(".welPanel").hide().eq(tarTab.index()).show();
	},
	pullData : function(){
		var that = this;
		var hash = this.getHash();
		var type = this.statics.type;
		if(!hash) return false;
		XHR.getReply(hash,type,{
			loading : function(){},
			removeLoading : function(){},
			success : function(res){
				var type = res.msg_type;
				var lists = null;
				var html = "";
				var tpl = $("#welReplyImageBoxTpl").html();
				var tabNavs = $("#welSwitchTabNavWrap .welTabNav");
				if(type=="TEXT"){ //纯文字
					lists = res.lists || "";
					$("#reply_welcome_textarea").val(lists);
					tabNavs.eq(0).click();
				}else{ //多图文
					lists = res.lists || [];
					if(lists.length){
						for(var i in lists){
							var json = lists[i];
							var data = {
								title : json.title || "",
								picUrl : json.img || "",
								digest : json.digest || "",
								url : json.source_url || ""
							};
							data["picUrlInp"] = json.img ? json.img : that.statics.defaultImg;
							html += that.parseTemplate(tpl,data);
						}
					}else{
						html += that.parseTemplate(tpl,{
							title : "",
							picUrl : "",
							picUrlInp : that.statics.defaultImg,
							digest : "",
							url : ""
						});
					}
					$("#addWelReplyBoxBtn").before(html);
					tabNavs.eq(1).click();
				}
			},
			fail : function(){},
			timeout : function(){},
			error : function(){}
		})
	},
	onSubmit : function(that,e){
		var tarTab = $("#welSwitchTabNavWrap").children(".active");
		var type = tarTab.attr('data-type');
		var hash = that.getHash();
		var submitBtn = that.submitBtn;
		var callbacks = {
			loading : function(){
				submitBtn.text("正在提交...").addClass("loading");
			},
			removeLoading : function(){
				submitBtn.text("提交").removeClass("loading");
			},
			success : function(res){
				PFT_GLOBAL.U.Alert("success",'<p style="width:160px">保存成功</p>')
			},
			fail : function(res){
				var msg = res.msg || "保存失败";
				PFT_GLOBAL.U.Alert("fail",'<p style="width:200px">'+msg+'</p>');
			},
			timeout : function(res){
				PFT_GLOBAL.U.Alert("fail",'<p style="width:160px;">请求超时</p>');
			},
			serverError : function(res){
				PFT_GLOBAL.U.Alert("fail",'<p style="width:160px;">请求出错</p>');
			}
		};
		if(!hash || submitBtn.hasClass("loading")) return false;
		if(type=="TEXT"){ //纯文字
			var txt = $("#reply_welcome_textarea").val();
			txt && XHR.submit.EVENT_SUBSCRIBE_TEXT(hash,$("#reply_welcome_textarea").val(),callbacks);
		}else{ //图文(可能有多条图文)
			var lists = [];
			$("#welReplyWrap .welReplyImageBox").each(function(){
				var box = $(this);
				var json = {};
				json["title"] = box.find(".reply_welcome_imgtxt_tit").val();
				json["img"] = box.find(".showimgPathInp").val();
				json["digest"] = box.find(".reply_welcome_imgtxt_digest").val();
				json["source_url"] = box.find(".reply_welcome_imgtxt_ourl").val();
				if(json.title && json.img && json.digest && json.source_url) lists.push(json);
			})
			that.validify() && XHR.submit.EVENT_SUBSCRIBE_IMG_TEXT(hash,lists,callbacks);
		}
	},
	validify : function(){
		var result = true;
		$("#welReplyWrap .welReplyImageBox input[data-verify]").each(function(){
			var tarInp = $(this);
			var val = tarInp.val();
			var rule = tarInp.attr("data-verify");
			if((rule=="require" && !val) || (rule=="require" && val=="http://")){
				tarInp.addClass("error").parent().next().addClass("error");
				result = false;
			}
		})
		return result;
	},
	onVerifyInpFocus : function(that,e){
		var tarInp = $(e.currentTarget);
		tarInp.removeClass("error").parent().next().removeClass("error");
	},
	//新添一条图文回复
	onAddWelReplyBox : function(that,e){
		var tpl = $("#welReplyImageBoxTpl").html();
		var tarBtn = $(e.currentTarget);
		var html = that.parseTemplate(tpl,{title:"",digest:"",picUrl:that.statics.defaultImg,url:"http://",picUrlInp : ""});
		tarBtn.before(html);
	},
	//删除一条图文回复
	onRemoveWelReplyBox : function(that,e){
		if($("#welReplyWrap").children(".welReplyImageBox").length==1) return false;
		var tarBtn = $(e.currentTarget);
		var box = tarBtn.parents(".welReplyImageBox");
		box.remove();
	},
	onOpenDialog : function(that,e){
		var tarBtn = $(e.currentTarget);
		var tarInp = tarBtn.parent().prev();
		var tarImg = tarBtn.parents(".input-group").next().find(".img-thumbnail");
		dialog.open(tarInp,tarImg);
	}
});